package cn.wellwan.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * Copyright (C),微动天下
 * FileName: RestTemplateConfigure
 * Author:   maokai
 * Date:     2018/12/25 14:47
 * Description: 注入请求工具
 */
@Configuration
public class RestTemplateConfigure {

    @Autowired
    private AutowireCapableBeanFactory beanFactory;
    @Autowired
    DefaultListableBeanFactory defaultListableBeanFactory;
    @Bean
    public RestTemplate getRestTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        //将new出的对象放入Spring容器中
        defaultListableBeanFactory.registerSingleton("restTemplate", restTemplate);
        //自动注入依赖
        beanFactory.autowireBean(restTemplate);
        return restTemplate;
    }
}
